package com.w3c.pragmatic.controller;

import com.w3c.pragmatic.config.AliOssConfig;
import com.w3c.pragmatic.domain.Response;
import com.w3c.pragmatic.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;

/**
 * 阿里云oss 前端控制器
 *
 * @author wangdong
 * @since 2025-02-20
 */
@RestController
@RequestMapping("/oss")
@Api(tags = "阿里云oss控制层OrderInfoController")
public class OssController {

  @Resource private AliOssConfig aliOssConfig;
  @Resource private AliOssUtil aliOssUtil;

  /**
   * 上传一个文件到oss服务器
   *
   * @return String 文件id
   */
  @ApiOperation(value = "文件上传到oss服务器", httpMethod = "POST")
  @RequestMapping(
      value = "/upload",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> uploadFileToOss(@RequestParam("file") MultipartFile file)
      throws IOException {
    // 获取文件的输入流
    InputStream inputStream = file.getInputStream();
    String filename = file.getOriginalFilename();
    // 调用文件上传方法
    return aliOssUtil.uploadFileOss(filename, inputStream);
  }

  /**
   * 下载oss服务器资源
   *
   * @return String 文件id
   */
  @ApiOperation(value = "下载oss服务器资源", httpMethod = "POST")
  @RequestMapping(
          value = "/download/{fileName}",
          method = RequestMethod.POST,
          produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> downloadFileFromOss(@ApiParam("文件名称") @PathVariable String fileName) {
    // 下载服务器资源到本地
    // 本地路径 C:\Users\pragm\Desktop\temp-doc
    String ossDirectory = "dev/agreement/" + fileName;
    String localLocation = "C:\\Users\\pragm\\Desktop\\temp-doc\\" + fileName;
    return aliOssUtil.downloadFileOss(aliOssConfig.getBucketName(), ossDirectory, localLocation);
  }

  /**
   * 下载oss服务器资源
   *
   * @return String 文件id
   */
  @ApiOperation(value = "删除oss服务器资源", httpMethod = "POST")
  @RequestMapping(
          value = "/delete/{fileName}",
          method = RequestMethod.POST,
          produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> deleteFileFromOss(@ApiParam("文件名称") @PathVariable String fileName) {
    // 下载服务器资源到本地
    // 本地路径 C:\Users\pragm\Desktop\temp-doc
    String ossDirectory = "dev/agreement/" + fileName;
    return aliOssUtil.deleteObject(aliOssConfig.getBucketName(), ossDirectory);
  }

  /**
   * 获取oss服务文件url(设置有效期)
   *
   * @return String 文件id
   */
  @ApiOperation(value = "获取oss服务文件url(设置有效期)", httpMethod = "POST")
  @RequestMapping(
      value = "/getUrl/{fileName}",
      method = RequestMethod.POST,
      produces = MediaType.APPLICATION_JSON_VALUE)
  public Response<String> getUrlExpire(@ApiParam("文件名称") @PathVariable String fileName) {
    // 获取有效期
    String ossDirectory = "dev/agreement/" + fileName;
    return aliOssUtil.getOssUrlExpire(aliOssConfig.getBucketName(), ossDirectory);
  }
}
